<?php
session_start();
require_once '../models/Users.php';

require_once '../models/_check_admin.php';

$userID= $_SESSION["session_user_id_admin"];
$row	= Users::getUserInfo($userID);
if(isset($_POST['btn_submit'])){
$_msg="";
$check=true;
$checkNull=true;
$checkPass=true;
	$password=$_POST["password"];
	$passwordNew=$_POST["passwordNew"];
	$passwordNew2=$_POST["passwordNew2"];
	if ($passwordNew!=$passwordNew2){
	$check=false;
	
	$_msg.="Hai mật khẩu không trùng nhau";
	}
	if ($password==null||$passwordNew==null){
	$checkNull=false;
	$_msg.="Các trường không được để trống.";
	}
	if($check==true&&$checkNull==true){
			$encryptpassword=Users::encryptpassword($password);
			$encryptpasswordNew=Users::encryptpassword($passwordNew);
			if($row['password']==$encryptpassword){
				
				$re=Users::resetPassword($userID,$encryptpasswordNew);
				if($re==true){
					$_SESSION["session_user_password"]=$encryptpasswordNew;
				//	$page="customer.php";
					//Customer::redirectTo($page);
					//$_msg.="chính xác.";
					echo "<script>alert('Thay mật khẩu thành công!');window.location='ql_user_admin.php'</script>";
				}
		}else{
				$_msg.="Mật khẩu không chính xác.";
			
			}
	}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Change Password</title>
<?php include '../admin/_meta.php';
require_once '../models/_check_admin.php';?>
</head>
<body>
<form method="post">
	<div id="firstLine">
		<div id="top-bar">
		<?php include '_top_bar_admin.php';?>
		</div>
	</div>
	<div id="container">
		<div id="header">
			<?php include '_menu_admin.php';?>
		</div>
		<div id="center">
			<div id="table-user">
				<table id="table-user-table">
						<tr>
							<th colspan="2">Change Password</th>
						</tr>
						<tr>
							<td>Old password:</td>
							<td><input type="password" name="password" class="btn_info" value="" placeholder="Old password"></td>
						</tr>
						<tr>
							<td>New password:</td>
							<td><input type="password" name="passwordNew" class="btn_info" value="" placeholder="New password"></td>
						</tr>
						<tr>
							<td>Enter the password:</td>
							<td><input type="password" name="passwordNew2" class="btn_info" value="" placeholder="Enter the password"></td>
						</tr>
						<tr>
							<td><input type="submit" class="btn_submit" name="btn_submit" value="Yes"></td>
							<td><a href="ql_user_admin.php"><input type="button" class="btn_submit" name="btn_submit" value="Cancel"></a></td>
						</tr>
						<tr><td><?php
							if(isset($_POST['btn_submit'])){
								//if($check==false||$checkNull==false||$checkPass==false){
									echo "<td colspan='2'>$_msg</td>";}
								//}
						?><td>
						</tr>
						</table>
			</div>
			</div>
		</div>
	</div>
	</div>
	<div>
		<?php include '_footer_admin.php';?>
	</div>
</form>
</body>
</html>